import router from './router'
// import store from './store'
// import { Message } from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import { getToken, getLoginBussinessPlatform } from '@/utils/auth' // get token from cookie
import getPageTitle from '@/utils/get-page-title'
// import { getRouters, generatedMenu, apiBindSceneId } from '@/api/dealRouter'
// import { allRouters, defaultConstRoutesLength } from '@/router/index'
// import store from '@/store/index'
import { BUSSINESS_PLATFORM, AccessUrl } from '@/consts'

NProgress.configure({ showSpinner: false }) // NProgress Configuration

// const whiteList = ['/login'] // no redirect whitelist
// const newRoute = generatedMenu(allRouters, JSON.parse(sessionStorage.getItem('routeData')))
// 路由过滤，钩子函数



router.beforeEach(async(to, from, next) => {
  // start progress bar
  NProgress.start()

  // set page title
  document.title = getPageTitle(to.meta.title)


  const hasToken = getToken()
  if (hasToken) {
    const bussinessPlatform = getLoginBussinessPlatform()
    if (bussinessPlatform) {
      switch (bussinessPlatform) {
        case BUSSINESS_PLATFORM.FRONT:
          window.location.replace(AccessUrl.frontUrl)
          break
        case BUSSINESS_PLATFORM.MANAGE:
          window.location.replace(AccessUrl.manageUrl)
          break
      }
      next()
    } else {
      next()
    }

    // if (to.path === '/login') {
    //   next({path: '/',replace: true})
    // } else {

    // }
  } else {
    next()
  }


  NProgress.done()
})

router.afterEach(() => {
  // finish progress bar
  NProgress.done()
})
